package com.wantupai.translate.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Toast;

import com.wantupai.translate.R;
import com.wantupai.translate.activity.camear.CameraActivity;
import com.wantupai.translate.base.BaseActivity;

/**
 * @author luxuchang
 * @date 2019/4/26 0026
 * @description
 */
public class SplashActivity extends BaseActivity {
    static final int CAMERA_REQUEST_CODE = 100;

    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    public void initViews() {
        checkPermission();
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void processClick(View v) {

    }

    private void checkPermission() {
        final String permission = Manifest.permission.CAMERA;  //相机权限
        final String permission1 = Manifest.permission.WRITE_EXTERNAL_STORAGE; //写入数据权限
        final String permission2 = Manifest.permission.READ_EXTERNAL_STORAGE; //写入数据权限
        final String permission3 = Manifest.permission.READ_PHONE_STATE; //读取手机状态
        if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, permission1) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, permission3) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, permission2) != PackageManager.PERMISSION_GRANTED) {  //先判断是否被赋予权限，没有则申请权限
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {  //给出权限申请说明
                ActivityCompat.requestPermissions(this,
                        new String[]{permission, permission1, permission2,permission3},
                        CAMERA_REQUEST_CODE);
            } else { //直接申请权限
                ActivityCompat.requestPermissions(this,
                        new String[]{permission, permission1, permission2,permission3},
                        CAMERA_REQUEST_CODE); //申请权限，可同时申请多个权限，并根据用户是否赋予权限进行判断
            }
        } else {  //赋予过权限，则直接调用相机拍照
            goNext();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED
                && grantResults[1] == PackageManager.PERMISSION_GRANTED
                && grantResults[3] == PackageManager.PERMISSION_GRANTED
                && grantResults[2] == PackageManager.PERMISSION_GRANTED) {  //如果用户赋予权限，则调用相机
            goNext();
        } else { //未赋予权限，则做出对应提示
            Toast.makeText(this,"权限被拒绝，请手动打开拍照和存储权限",Toast.LENGTH_SHORT).show();
            finish();
        }
    }

    private void goNext(){
        new android.os.Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(SplashActivity.this, CameraActivity.class));
                finish();
            }
        }, 2000);
    }
}
